package com.sc.nft.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.sc.nft.admin.request.MallActivityRankingEditRequest;
import com.sc.nft.admin.request.MallActivityRankingRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.MallActivityRanking;
import com.sc.nft.service.MallActivityRankingService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/activity/ranking")
@Api(tags = "活动排行榜模块")
public class ActivityRankingController {

    private final MallActivityRankingService mallActivityRankingService;

    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "排行榜列表", notes = "排行榜列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<MallActivityRanking> page(@Valid @RequestBody PageRequest request) {
        return PageResult.success(mallActivityRankingService.queryActivityRankingPage(request.toMp()), MallActivityRanking.class);
    }

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加排行榜", notes = "添加排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result add(@Valid @RequestBody MallActivityRankingRequest request) {
        MallActivityRanking mallActivityRanking = BeanUtil.copyProperties(request, MallActivityRanking.class);
        mallActivityRankingService.save(mallActivityRanking);
        return Result.success();
    }

    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑排行榜", notes = "编辑排行榜", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result edit(@Valid @RequestBody MallActivityRankingEditRequest request) {
        MallActivityRanking mallActivityRanking = BeanUtil.copyProperties(request, MallActivityRanking.class);
        mallActivityRankingService.update(mallActivityRanking);
        return Result.success();
    }
}
